package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.pojo.Depts;
import org.example.pojo.Result;
import org.example.server.DeptServer;
import org.example.server.EmpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class DeptController {

    @Autowired
    private DeptServer deptServer;

    /**
     * 查询部门列表
     * @return
     */
    @GetMapping("/depts")
    @Log
    public Result list(){
        log.info("查询部门列表");
        return Result.success(deptServer.list());
    }

    /**
     * 删除部门
     * @return
     */
    @DeleteMapping("/depts/{id}")
    @Log
    public Result delete(@PathVariable Integer id){
        log.info("删除部门");
        deptServer.delete(id);
        return Result.success();
    }

    @PostMapping("/depts")
    @Log
    public Result add(@RequestBody Depts depts){
        log.info("添加部门");
        deptServer.add(depts);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    @Log
    public Result get(@PathVariable Integer id){
        log.info("查询部门");
        return Result.success(deptServer.getById(id));
    }

    @PutMapping("/depts")
    @Log
    public Result update(@RequestBody Depts depts){
        log.info("修改部门");
        try {
            deptServer.update(depts);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败,修改前后是否一样，如果无法解决请联系管理员");
        }
    }
}
